﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MT.Common
{
    public class PageModel<T>
    {
        public PageModel()
        {
        }
        public PageModel(int pageIndex, int pageSize, List<T> item, int total)
        {
            this.PageSize = pageSize;
            this.PageIndex = pageIndex;
            this.Items = item;
            Total = total;
            SetTotal(total);
        }
        public int PageIndex { get; set; }

        public int PageSize { get; set; }

        public int TotalPage { get; set; }

        public bool IsLastPage { get; set; }

        public long Total { get; set; }

        // public int LastId { get; set; }

        public List<T> Items { get; set; }

        /// <summary>
        /// 附加参数
        /// </summary>
        public string Attach { get; set; }

        public void SetTotal(long total)
        {
            this.Total = total;


            if (PageSize > 0 && Total % PageSize == 0)
            {
                TotalPage = (int)(Total / PageSize);
            }
            else
            {
                TotalPage = PageSize > 0 ? (int)(Total / PageSize + 1) : 0;
            }
            if (PageIndex > TotalPage)
                PageIndex = TotalPage;
            if (PageIndex < 1)
            {
                PageIndex = 1;
            }

            IsLastPage = PageIndex >= TotalPage;
        }

       
    }
}
